/*
 * @(#)NeihantuXml.java		       Project:Tools
 * Date:2012-4-11
 *
 * Copyright (c) 2011 CFuture09, Institute of Software, 
 * Guangdong Ocean University, Zhanjiang, GuangDong, China.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sinaapp.msdxblog.tools.xml.neihantu;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

/**
 * @author Geek_Soledad (66704238@51uc.com)
 */
public class NeihantuXml {
	private String path;
	public static final String ATTR_NAME = "name";
	private static final String CHILD = "tip";
	private static final String ROOT = "tips";
	private String[] mFileNames;
	private String[] mNames;
	private String[] mNewNames;
	private String from;
	private String to;

	public NeihantuXml(String path) {
		this.path = path;
		extractFileNames();
		mNewNames = new String[mFileNames.length];
		changeFileNames();
		generatorXml();
	}

	/**
	 * 从图片的名字得到提示内容
	 */
	public void extractFileNames() {
		File file = new File(path);
		mFileNames = file.list(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				final String n = name.toLowerCase();
				if (n.endsWith(".gif") || n.endsWith(".png")
						|| n.endsWith(".jpg") || n.endsWith(".jpeg")) {
					return true;
				}
				return false;
			}
		});
		mNames = new String[mFileNames.length];
		for (int i = 0; i < mFileNames.length; i++) {
			mNames[i] = mFileNames[i].substring(0, mFileNames[i].indexOf('.'));
			System.out.println(new StringBuilder("文件").append(i).append("---")
					.append(mNames[i]));
		}
	}

	/**
	 * 获取新路径
	 * 
	 * @param from
	 * @return
	 */
	private String getNewPath(String from) {
		String newPath = from + "news";
		int i = 0;
		while (new File(newPath + i).exists()) {
			i++;
		}
		return newPath + i + File.separator;
	}

	/**
	 * 修改所有图片的名字
	 */
	public void changeFileNames() {
		try {
			File fileFrom;
			File fileTo;
			from = path + File.separator;
			to = getNewPath(from);
			for (int i = 0; i < mFileNames.length; i++) {
				mNewNames[i] = "p" + i;
				fileFrom = new File(from + mFileNames[i]);
				fileTo = new File(new StringBuilder(to)
						.append(mNewNames[i])
						.append(mFileNames[i].substring(mFileNames[i]
								.indexOf('.'))).toString());
				FileUtils.copyFile(fileFrom, fileTo);
				System.out.println(fileFrom.getAbsolutePath() + "---已复制到---"
						+ fileTo.getAbsolutePath());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 生成xml文件。
	 */
	public void generatorXml() {
		Element root = new Element(ROOT);
		for (int i = 0, length = mNames.length; i < length; i++) {
			Element content = new Element(CHILD);
			Attribute id = new Attribute(ATTR_NAME, mNewNames[i]);
			content.setAttribute(id);
			content.setText(mNames[i]);
			root.addContent(content);
		}
		XMLOutputter outputter = new XMLOutputter();
		Document doc = new Document(root);
		try {
			outputter.output(doc, new FileOutputStream(
					new File(to + "tips.xml")));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		// 这里的路径为图片所在的文件夹所对应的路径，该操作会对文件中的所有文件进行重命名，请先备份。
		if (args.length > 0) {
			new NeihantuXml(args[0]);
			return;
		} else {
			System.out.println("请在后面加入 需要更名的文件夹：如：");
			System.out
					.println("java -jar NeihantuXml C:/Users/msdx/Desktop/pictures");
			System.out.println("当参数为\".\"时，表示对当前目录使用");
		}
	}
}
